package cn.homework.dao.impl;

import cn.homework.dao.ProductDao;
import cn.homework.domain.Customers;
import cn.homework.domain.Product;
import cn.homework.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author ACT
 */
public class ProductDaoImpl implements ProductDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Product> findProductCommonList() {
        String sql = "select * from product order by price desc limit 0 , 8";
        List<Product> list = template.query(sql, new BeanPropertyRowMapper<>(Product.class));
        return list;
    }

    @Override
    public List<Product> findProductDiscounted() {
        String sql = "select * from product order by price asc limit 0 , 5";
        List<Product> list = template.query(sql, new BeanPropertyRowMapper<>(Product.class));
        return list;
    }

    @Override
    public List<Product> findProductHot() {
        String sql = "select * from product order by price desc limit 8 , 8";
        List<Product> list = template.query(sql, new BeanPropertyRowMapper<>(Product.class));
        return list;
    }

    @Override
    public List<Product> findProductQuery(Map<String, String[]> map) {
        String sql = "select * from product  where 1 = 1 ";
        StringBuilder sb = new StringBuilder(sql);
        //遍历map
        Set<String> keySet = map.keySet();
        //定义参数的集合
        List<Object> params = new ArrayList<>();
        for (String key : keySet) {
            //排除条件信息
            if ("demand".equals(key) || "src".equals(key) || "id".equals(key)) {
                continue;
            }

            //获取values
            String values = map.get(key)[0];
//            System.out.println("key" + key);
//            System.out.println("values" + values);
            //判断是否有值
            if (values != null && !"".equals(values)) {
                //有值
                sb.append(" and " + key + " like ? ");
                params.add("%" + values + "%");
            }
        }
        sb.append(" limit ? , ? ");
        params.add(0);
        params.add(8);
        sql = sb.toString();
//        System.out.println("sql" + sql);
//        System.out.println("params" + params);
        return template.query(sql, new BeanPropertyRowMapper<Product>(Product.class), params.toArray());
    }

    @Override
    public List<Product> findProductByKitchen() {
        String sql = "select * from product where type like '厨卫系列' order by price desc limit 0 , 8";
        List<Product> list = template.query(sql, new BeanPropertyRowMapper<>(Product.class));
        return list;
    }

    @Override
    public List<Product> findProductByWashing() {
        String sql = "select * from product where type like '洗衣机系列' order by price desc limit 0 , 8";
        List<Product> list = template.query(sql, new BeanPropertyRowMapper<>(Product.class));
        return list;
    }

    @Override
    public List<Product> findProductByComputer() {
        String sql = "select * from product where type like '电脑专区' order by price desc limit 0 , 8";
        List<Product> list = template.query(sql, new BeanPropertyRowMapper<>(Product.class));
        return list;
    }

    @Override
    public List<Product> findProductByTV() {
        String sql = "select * from product where type like '电视机系列' order by price desc limit 0 , 8";
        List<Product> list = template.query(sql, new BeanPropertyRowMapper<>(Product.class));
        return list;
    }

    @Override
    public List<Product> findProductAllShow() {
        String sql = "select * from product order by price desc";
        List<Product> list = template.query(sql, new BeanPropertyRowMapper<Product>(Product.class));
        return list;
    }

    @Override
    public List<Product> findProductAllKitchenShow() {
        String sql = "select * from product where type like '厨卫系列' order by price desc";
        List<Product> list = template.query(sql, new BeanPropertyRowMapper<Product>(Product.class));
        return list;
    }

    @Override
    public List<Product> findProductAllWashingShow() {
        String sql = "select * from product where type like '洗衣机系列' order by price desc";
        List<Product> list = template.query(sql, new BeanPropertyRowMapper<Product>(Product.class));
        return list;
    }

    @Override
    public List<Product> findProductAllComputerShow() {
        String sql = "select * from product where type like '电脑专区' order by price desc";
        List<Product> list = template.query(sql, new BeanPropertyRowMapper<Product>(Product.class));
        return list;
    }

    @Override
    public List<Product> findProductAllTVShow() {
        String sql = "select * from product where type like '电视机系列' order by price desc";
        List<Product> list = template.query(sql, new BeanPropertyRowMapper<Product>(Product.class));
        return list;
    }

    @Override
    public Product findProductDetailById(String id) {
        String sql = "select * from product where id = ?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Product.class), id);
    }
}
